from copy import copy

from openpyxl import load_workbook
from datetime import datetime, timedelta

# 计算昨天的日期，并格式化为工作表名称的一部分
yesterday_str = (datetime.now() - timedelta(days=1)).strftime("%m%d")
new_sheet_name = f"拷机日报-{yesterday_str}"

# 拷机记录表的路径
workbook_path = r'\\192.168.253.179\firmware_Submit_Test\log\NVR拷机日志\拷机记录表.xlsx'

# 载入工作簿
wb = load_workbook(workbook_path)

# 复制指定sheet内容到新sheet
source_sheet_name = 'template'  # 请根据实际需要复制的sheet名进行修改
# 1. 复制模板Sheet
if source_sheet_name in wb.sheetnames:
    template_sheet = wb[source_sheet_name]
    report_sheet = wb.copy_worksheet(template_sheet)
    report_sheet.title = new_sheet_name
else:
    print("Template sheet not found.")
    exit()
    print(f"Source sheet '{source_sheet_name}' not found.")
new_sheet = wb[new_sheet_name]

# 先获取数据
from get_performance_result import get_performance_result
from get_log_result import main

per_res = get_performance_result(r'\\192.168.253.179\firmware_Submit_Test\log\NVR拷机日志\性能监控\system_usage.xlsx')
log_res = main()
# 遍历u2中的每个键和值
for key, value in log_res.items():
    if key in per_res:
        # 如果u1中已经有了这个键，更新它的值
        per_res[key].update(value)
    else:
        # 如果u1中没有这个键，添加它
        per_res[key] = value

updates = per_res
for model, updates in updates.items():
    for row in new_sheet.iter_rows(min_row=2, max_col=8):
        if row[0].value == model:  # 假设型号在第一列
            # 假设“硬盘信息”、“CPU监控”、“内存监控”和“昨日日志”位于G列，本次数据和错误信息在H列和I列
            for cell in row:
                if cell.value in updates:
                    current_row = cell.row
                    col_num = cell.column
                    while True:
                        cur_cell = new_sheet[current_row][col_num - 1]
                        if cur_cell.value in updates:
                            update_col = col_num + 2  # 本次数据列
                            error_col = col_num + 3  # 错误信息列
                            new_sheet.cell(row=cur_cell.row, column=update_col).value = updates[cur_cell.value][0]
                            new_sheet.cell(row=cur_cell.row, column=error_col).value = updates[cur_cell.value][1]
                        if cur_cell.value == '昨日日志':
                            break
                        current_row += 1
                    break

# # 更新信息到新工作表中
# for row in new_sheet.iter_rows(min_row=2, values_only=False):
#     model_cell = row[0]  # 假设型号在第一列
#     if model_cell.value in updates:
#         model_updates = updates[model_cell.value]
#         for cell in row:
#             if cell.value in model_updates:
#                 # 找到标签对应的合并单元格
#                 merge_range = new_sheet.merged_cell_ranges[cell.coordinate]
#                 if merge_range:
#                     start_col = cell.column + 1  # 假设数据在标签右侧的单元格
#                     new_sheet.cell(row=cell.row, column=start_col).value = model_updates[cell.value]
#                     # 根据需要更新错误信息到下一个单元格
#                     new_sheet.cell(row=cell.row, column=start_col + 1).value = "错误信息（如果有）"

# 保存工作簿
wb.save(workbook_path)
